package com.zx.jhpt;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.zx.jhpt.datamart.DataMart;
import com.zx.jhpt.datamart.MarketEngine;
import com.zx.jhpt.service.MarketEngineService;
import org.apache.log4j.Logger;

import com.zx.framework.plat.dept.po.Dept;
import com.zx.framework.plat.dept.service.DeptService;
import com.zx.jhpt.excel.Engine;
import com.zx.jhpt.excel.vo.Risk;
import com.zx.jhpt.po.Fxfkjz;
import com.zx.jhpt.service.EngineService;
import com.zx.jhpt.service.FxfkjzService;
import com.zx.jhpt.util.DeptCache;
import com.zx.jhpt.util.FxdCache;

public class Startup {
    private static final Logger LOGGER = Logger.getLogger(Startup.class);
    @Resource
    private FxfkjzService fxfkjzService;
    @Resource
    private EngineService engineService;
    @Resource
    private MarketEngineService marketEngineService;
    @Resource
    private DeptService deptService;

    @PostConstruct
    public void init() {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("风险矩阵 cache loading!");
        }
        List<Fxfkjz> fxfkjzs = fxfkjzService.getAll();
        for (Fxfkjz fxfkjz : fxfkjzs) {
            try {
            	System.out.println(fxfkjz.getId());
                Risk risk = Engine.getRisk(fxfkjz.getId());
                String sql = Engine.createTable(risk);
                if(fxfkjz.getId().equals("SJZYZFXFK-3-9")){
                	System.out.println("11");
                }
                if (!engineService.exists(risk.getTbName())) {
                    engineService.executeCreateSQL(sql);
                }
            } catch (Exception e) {
                LOGGER.error("风险点初始化失败:" + e.getMessage(), e);
            }
        }

        List<Dept> depts = deptService.findAll("orders");
        DeptCache.init(depts);

        FxdCache.init(fxfkjzs);
        /**
         * 1.遍历WEB-INF下log文件夹内所有*.log文件
         * if table exists
         * 		needn't create
         * 		return
         * else
         * 		2.logJson2DataMart
         * 		3.createTable
         */
//        if (LOGGER.isDebugEnabled()) {
//            LOGGER.debug("Log cache loading!");
//        }
//        try {
//            List<String> logFileNames = MarketEngine.scanLogFile();
//            for (String logFileName : logFileNames) {
//                DataMart dataMart = MarketEngine.getDataMart(logFileName);
//                String sql = MarketEngine.createTableSql(dataMart);
//                if (!marketEngineService.exists(dataMart.getTableName())) {
//                    marketEngineService.executeCreateSQL(sql);
//                }
//            }
//        } catch (Exception e) {
//            LOGGER.error("LOG日志初始化失败:" + e.getMessage(), e);
//        }
    }
}
